package ua.org.nuos.wclientgui;

import org.apache.wicket.Page;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.http.WebRequest;
import org.wicketstuff.javaee.injection.JavaEEComponentInjector;
import org.wicketstuff.wicket7.util.watch.Nio2ModificationWatcher;
import ua.org.nuos.wclientgui.pages.MainPage;
import ua.org.nuos.wclientgui.pages.StartPage;
import ua.org.nuos.wclientgui.utils.ClientAuthenticatedWebSession;
import ua.org.nuos.wclientgui.utils.ClientAuthorizer;
import ua.org.nuos.wclientgui.utils.ServiceLocator;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: dio
 * Date: 9/16/12
 * Time: 1:08 PM
 * To change this template use File | Settings | File Templates.
 */
public class ClientApplication extends AuthenticatedWebApplication {

    private HttpServletRequest request;
    private ServiceLocator serviceLocator;

    public HttpServletRequest getRequest() {
        return request;
    }

    public ServiceLocator getServiceLocator() {
        return serviceLocator;
    }

    @Override
    protected void init() {
        serviceLocator = new ServiceLocator();
        getSecuritySettings().setAuthorizationStrategy(new ClientAuthorizer(serviceLocator));

        JavaEEComponentInjector injector = new JavaEEComponentInjector(this);
        injector.inject(serviceLocator);
        getComponentInstantiationListeners().add(injector);

        getResourceSettings().setResourceWatcher(new Nio2ModificationWatcher(this));

        mountPage("/start", StartPage.class);
        mountPage("/main", MainPage.class);
    }

    @Override
    protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {
        return ClientAuthenticatedWebSession.class;
    }

    @Override
    protected Class<? extends WebPage> getSignInPageClass() {
        return StartPage.class;
    }

    @Override
    public Class<? extends Page> getHomePage() {
        return StartPage.class;
    }

    @Override
    public WebRequest newWebRequest(HttpServletRequest servletRequest, final String filterPath) {
        request = servletRequest;
        return super.newWebRequest(servletRequest, filterPath);
    }
}
